package relu;

import jfm.lp.LPX;
import jfm.model.Types.*;
import jfm.model.BadModelException;
import jfm.model.Crop;
import jfm.model.CroppingComponent;
import jfm.model.Farm;
import jfm.model.GLPKException;
import jfm.model.Output;
import jfm.model.Types;
import jfm.mou.*;
import java.io.*;

import relu.R.landscape.*;
import relu.R.weeds.IFSSorter;
import relu.R.*;
import org.apache.commons.math.random.*;

public class App {
	public static void main(String[] args){
		int[] farmAreas=new int[]{1000,1000,1000};
		String[] templateFiles=new String[] {"fh1.xml","fh1.xml","fh1.xml"};
		String[] mouFiles=new String[] {"mfh1.xml","mfh2.xml","mfh3.xml"};

		String[] priceScenarios=new String[]{"2007","1","2","3","4","5","6","7","8","9","2008"};
		
		EnviroVsProfitDistribution sDist=new EnviroVsProfitDistribution();
		sDist.setCropPriceScenarios(args[0]+".csv");
//		ls.relaxLocations(1, 0.8);
//		ls.addFarm(244,"FP1.xml", "mouFP1.xml");
//		ls.addFarm(490,"FP2.xml", "mouFP2.xml");
//		ls.addFarm(3150,"FP3.xml", "mouFP3.xml");
		try{
			NonSpatialLandscape ls=new NonSpatialLandscape();	
			for(int f=0;f<3;f++){ // Loop farm types 
				ls.addFarm(farmAreas[f], templateFiles[f], mouFiles[f]);
			}
			ls.createAndDistributeFields(300);
			IFSSorter ifs=new IFSSorter();
			ifs.initFromCSV("IFSWeeds.csv");
			ls.createWeedPopulations(ifs);
			ls.solveFarmLPs();
	//		ls.equilibrateWeedPopulations(100);
			String fileName=(args[0]+"out.csv");
			FileWriter writer = new FileWriter(fileName);
			FileWriter cropWriter=new FileWriter(args[0]+"cropsOut.csv");
			cropWriter.append("scenario,farm,"+SpatialLandscape.oneLineCSVHeader()+"\n");
			writer.append("scenario,numBirdsStart");
			for( int f=0;f<3;f++){
				writer.append(",farm"+f);
			}
			writer.append("\n");
			for(int pr=0;pr<10;pr++){
				System.out.println("Solving for scenario "+priceScenarios[pr]);
				sDist.setCropPricesForScenario(priceScenarios[pr]);			
				ls.applySocioEconomicDistribution(sDist);
				ls.solveFarmLPs();
				ls.equilibrateWeedPopulations(100);
				for(int frmi=0;frmi<3;frmi++){
					cropWriter.append(pr+","+frmi+","+ls.oneLineCSVSummary(frmi)+"\n");
					cropWriter.flush();
				}
				
				/** To do a pop dynamics run */
				ls.stepSummer(50);
				for(int t=0;t<200;t++){
					ls.stepSummer(-1);
					ls.stepWinter();
					writer.append(pr+","+t);
					for(int frmi=0;frmi<3;frmi++){
						writer.append(","+ls.numBirds(frmi));
					}
					System.out.println(ls.numBirds());
					writer.append("\n");
					writer.flush();
				}
				
				/* To generate survival curves 
				for( int nBirds=1;nBirds<100;nBirds++){
					int actualBirds=nBirds*6;					
					ls.stepSummer(actualBirds);
					ls.stepWinter();
					writer.append(pr+","+actualBirds);
					for(int frmi=0;frmi<3;frmi++){
						writer.append(","+ls.numBirds(frmi));
					}
					writer.append("\n");
					writer.flush();
				}*/
			}
			writer.close();
			cropWriter.close();
		} catch (Exception ex){
			ex.printStackTrace();
			throw new Error(ex.getMessage());
		}

		
	}
}
